import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;

public class GeLuoBuSi extends Method{

	private float g0 = 0;		// 从表中查出的g0 值
	private final Hashtable<String, Float> G0 = new Hashtable<String, Float>();
	
	public GeLuoBuSi(ArrayList<Float> data,int format) {
		this(data,0.05f, format);
	}

	public GeLuoBuSi(ArrayList<Float> data,float alpha, int format) {
		super(data,format);
		this.name = "格罗布斯准则";
		initG0();
		this.g0 = getG0(data.size(),alpha);
	}
	
	@Override
	protected boolean hasGrossError(){
		Collections.sort(data);
		float avarage = calAvarage(data);
		float sigma = calSigma(data);
		if(!((avarage-data.get(0))/sigma < g0)){
			blackList.add(data.get(0));
			return true;
		}
		if(!((data.get(data.size()-1)-avarage)/sigma < g0)){
			blackList.add(data.get(data.size()-1));
			return true;
		}
		return false;
	}
	
	/**
	 * 	查表已取得g0
	 */
	private float getG0(int n, float alpha){
		if(n<3) n=3;
		if(n>25&& n<30) n=25;
		if(n>30&& n<35) n=30;
		if(n>35&& n<40) n=35;
		if(n>40&& n<50) n=40;
		if(n>50&& n<100) n=50;
		if(n>100) n=100;
		return G0.get(n+"&"+alpha);
	}
	
	/**
	 *	初始化表
	 */
	private final void initG0(){
		G0.put("3&0.05", 1.15f);			G0.put("3&0.01", 1.16f);
		G0.put("4&0.05", 1.46f);			G0.put("4&0.01", 1.49f);
		G0.put("5&0.05", 1.67f);			G0.put("5&0.01", 1.75f);
		G0.put("6&0.05", 1.82f);			G0.put("6&0.01", 1.94f);
		G0.put("7&0.05", 1.94f);			G0.put("7&0.01", 2.10f);
		G0.put("8&0.05", 2.03f);			G0.put("8&0.01", 2.22f);
		G0.put("9&0.05", 2.11f);			G0.put("9&0.01", 2.32f);
		G0.put("10&0.05", 2.18f);		G0.put("10&0.01", 2.41f);
		G0.put("11&0.05", 2.23f);		G0.put("11&0.01", 2.48f);
		G0.put("12&0.05", 2.28f);		G0.put("12&0.01", 2.55f);
		G0.put("13&0.05", 2.33f);		G0.put("13&0.01", 2.61f);
		G0.put("14&0.05", 2.37f);		G0.put("14&0.01", 2.66f);
		G0.put("15&0.05", 2.41f);		G0.put("15&0.01", 2.70f);
		G0.put("16&0.05", 2.44f);		G0.put("16&0.01", 2.75f);
		G0.put("17&0.05", 2.48f);		G0.put("17&0.01", 2.78f);
		G0.put("18&0.05", 2.50f);		G0.put("18&0.01", 2.82f);
		G0.put("19&0.05", 2.53f);		G0.put("19&0.01", 2.85f);
		G0.put("20&0.05", 2.56f);		G0.put("20&0.01", 2.88f);
		G0.put("21&0.05", 2.58f);		G0.put("21&0.01", 2.91f);
		G0.put("22&0.05", 2.60f);		G0.put("22&0.01", 2.94f);
		G0.put("23&0.05", 2.62f);		G0.put("23&0.01", 2.96f);
		G0.put("24&0.05", 2.64f);		G0.put("24&0.01", 2.99f);
		G0.put("25&0.05", 2.66f);		G0.put("25&0.01", 3.01f);
		G0.put("30&0.05", 2.74f);		G0.put("30&0.01", 3.10f);
		G0.put("35&0.05", 2.81f);		G0.put("35&0.01", 3.18f);
		G0.put("40&0.05", 2.87f);		G0.put("40&0.01", 3.24f);
		G0.put("50&0.05", 2.96f);		G0.put("50&0.01", 3.34f);
		G0.put("100&0.05", 3.17f);		G0.put("100&0.01", 3.59f);
	}
}
